import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

@WebServlet("/download")
public class download extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        File file = new File(req.getServletContext().getRealPath("/www.html"));
//        File file = new File("C:\\Users\\陈宇森\\IdeaProjects\\webapp\\src\\main\\java\\download.java");
        resp.setHeader("Content-Disposition", "attachment; filename=www.html");
        FileInputStream input = new FileInputStream(file);
        ServletOutputStream output = resp.getOutputStream();
        byte[] bytes = new byte[1024];
        int len;
        while ((len=input.read(bytes))!=-1){
            output.write(bytes,0,len);
        }
        output.flush();
        output.close();
    }
}
